// pages/logs/index.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weekText: {
      type: Array,
      value: ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  },
  lastMonth:'◀',
  nextMonth:  '▶',
  clockData:null,
  clockState:1 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.setData({
      platform:app.globalData.platform,
      city:app.globalData.city,
       width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop,
      domain:app.globalData.domain,
      appUrl:app.globalData.appUrl
    })
    
  },
  
  scrollCalendar(year, month, date){
    console.log(year, month, date)
    var that = this, scrollLeft = 0;
    wx.getSystemInfo({
        success(res) {
            //切换月份时 date为0
            if (date == 0 ){
                scrollLeft = 0;
                //切换到当年当月 滚动到当日
                if (year == that.data.YEAR && month == that.data.MONTH) {
                    scrollLeft = that.data.DATE * 45 - res.windowWidth / 2 - 22.5;
                }
            }else{
                // 点选具体某一天 滚到到指定日期
                scrollLeft = date * 45 - res.windowWidth / 2 - 22.5;
            }

            that.setData({
                scrollLeft: scrollLeft
            })
        }
    })
},
getclockDate:function(year,month,day){
  let that=this;
  var open=wx.getStorageSync('open');
  var member_id=open.member_id;
  wx.request({
   // url: 'https://www.lypb.com/xiehui/clockData.php',
    url:app.globalData.domain+'member/clock/index',
    data:{year:year,month:month,member_id:member_id},
    success:function(res){
      console.log(res);
        that.setData({
          clockData:res.data
        })
    }
  })

},
goCard:function(){
  wx.redirectTo({
    url: 'card',
  })
},
  display: function (year, month, date) {
    this.setData({
        year,
        month,
        date,
        title: year + '年' + this.zero(month) + '月'
    })
    this.getclockDate(year,month,date);
    this.createDays(year, month);
    this.createEmptyGrids(year, month);

    //滚动模糊 初始界面
    this.scrollCalendar(year, month, date);
    //this.getclockDate(year,month,0);
},
  today: function () {
    let DATE = this.data.defaultValue?new Date(this.data.defaultValue) : new Date(),
        year = DATE.getFullYear(),
        month = DATE.getMonth() + 1,
        date = DATE.getDate(),
        select = year + '-' + this.zero(month) + '-' + this.zero(date);

    this.setData({
        format: select,
        select: select,
        year: year,
        month: month,
        date: date,
        YEAR: year,
        MONTH: month,
        DATE: date,
    })

    //初始化日历组件UI
    this.display(year, month, date);

    //发送事件监听
    this.triggerEvent('select', select);
},
select: function (e) {
  console.log(e);
  let date = e.currentTarget.dataset.date;
  let clock = e.currentTarget.dataset.clock;
  let clock_id = e.currentTarget.dataset.clock_id;

  var  select = this.data.year + '-' + this.zero(this.data.month) + '-' + this.zero(date);
  this.setData({
      title: this.data.year + '年' + this.zero(this.data.month) + '月' + this.zero(date) + '日',
      select: select,
      year: this.data.year,
      month: this.data.month,
      date:date
  });
  if(clock==1){
    this.setData({
      clockState:1,
      clock_id:clock_id
    })
  }
  //发送事件监听
  this.triggerEvent('select', select);

  //滚动日历到选中日期
  this.scrollCalendar(this.data.year, this.data.month,date);
  
},
//上个月
lastMonth: function () {
  let month = this.data.month == 1 ? 12 : this.data.month - 1;
  let year = this.data.month == 1 ? this.data.year - 1 : this.data.year;
  //初始化日历组件UI
  this.display(year, month, 0);
},
//下个月
nextMonth: function () {
  let month = this.data.month == 12 ? 1 : this.data.month + 1;
  let year = this.data.month == 12 ? this.data.year + 1 : this.data.year;
  //初始化日历组件UI
  this.display(year, month, 0);
},
//获取当月天数
getThisMonthDays: function (year, month) {
  return new Date(year, month, 0).getDate();
},
// 绘制当月天数占的格子
createDays: function (year, month) {
  let thisMonthDays = [],
      days = this.getThisMonthDays(year, month);
  for (let i = 1; i <= days; i++) {
      thisMonthDays.push({
          date: i,
          dateFormat: this.zero(i),
          monthFormat: this.zero(month),
          week: this.data.weekText[new Date(Date.UTC(year, month - 1, i)).getDay()]
      });
  }
  this.setData({
      thisMonthDays
  })
},
//获取当月空出的天数
createEmptyGrids: function (year, month) {
  let week = new Date(Date.UTC(year, month - 1, 1)).getDay(),
      empytGridsBefore = [],
      empytGridsAfter = [],
      emptyDays = (week == 0 ? 7 : week);
  //当月天数
  var thisMonthDays = this.getThisMonthDays(year, month);
  //上月天数
  var preMonthDays = month - 1 < 0 ?
       this.getThisMonthDays(year - 1, 12) 
      : this.getThisMonthDays(year, month - 1);

  //空出日期
  for (let i = 1; i <= emptyDays; i++) {
      empytGridsBefore.push(preMonthDays - (emptyDays - i));
  }

  var after = (42 - thisMonthDays - emptyDays) - 7 >= 0 ?
               (42 - thisMonthDays - emptyDays) - 7 
              : (42 - thisMonthDays - emptyDays);
  for (let i = 1; i <= after; i++) {
      empytGridsAfter.push(i);
  }
  this.setData({
      empytGridsAfter,
      empytGridsBefore
  })
},

//补全0
zero: function (i) {
  return i >= 10 ?i : '0' + i;
},
gotoDetail:function(){
  var clock_id=this.data.clock_id;
  wx.navigateTo({
    url: 'shareclock_id='+clock_id,
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this;
    /*
   wx.request({
     url: 'https://www.lypb.com/xiehui/access_token.php',
     success:function(res){
       console.log(res.data)
     }
   })
   */
  var open=wx.getStorageSync('open');
  var member_id=open.member_id;
  this.today();
   wx.request({
    // url: 'https://www.lypb.com/xiehui/getClockState.php',
    
    url:app.globalData.domain+'member/clock/clockState',
    data:{member_id:member_id},
    // data:{union_id:app.globalData.open.union_id},
     success:function(res){
       console.log(res.data);
       that.setData({
         clockState:res.data.clock_state,
         clockDay:res.data.days,
         clockDistance:res.data.distance,
         clockPaceMinute:res.data.pace_minute,
         clockPaceSecond:res.data.pace_second,
         clock_id:res.data.clock_id
       })
     }
   })
  },
  goBack:function(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  goBack:function(){
    wx.navigateBack();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})